﻿using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;

namespace JackODummy
{
	public class SubsystemJackODummy : SubsystemBlockBehavior
	{
		public SubsystemPickables SubsystemPickables
		{
			get; set;
		}
		public override int[] HandledBlocks => [ BlocksManager.GetBlockIndex<AirBlock>() ];
		public override void Load(ValuesDictionary valuesDictionary)
		{
			base.Load(valuesDictionary);
			SubsystemPickables = Project.FindSubsystem<SubsystemPickables>(throwOnError: true);
		}
		public override bool OnUse(Ray3 ray, ComponentMiner componentMiner)
		{
			int jackODummyBlockIndex = BlocksManager.GetBlockIndex<JackODummyBlock>();
			int id = Terrain.ExtractContents(componentMiner.ActiveBlockValue);
			if(id == BlocksManager.GetBlockIndex<AirBlock>())
			{
				BodyRaycastResult? bodyRaycastResult = componentMiner.Raycast<BodyRaycastResult>(ray, RaycastMode.Interaction);
				if (bodyRaycastResult.HasValue)
				{
					ComponentJackODummy componentJackODummy = bodyRaycastResult.Value.ComponentBody.Entity.FindComponent<ComponentJackODummy>();
					if (componentJackODummy != null)
					{
						SubsystemPickables.AddPickable(jackODummyBlockIndex, 1, componentJackODummy.ComponentBody.Position + 0.15f * Vector3.UnitY, Vector3.UnitY, null);
						Project.RemoveEntity(componentJackODummy.Entity, disposeEntity: true);
						return true;
					}
				}
			}
			else if(id == jackODummyBlockIndex)
			{
				TerrainRaycastResult? terrainRaycastResult = componentMiner.Raycast<TerrainRaycastResult>(ray, RaycastMode.Digging);
				if (terrainRaycastResult.HasValue)
				{
					Vector3 hitPoint = terrainRaycastResult.Value.HitPoint();
					Vector3 position = new Vector3(Terrain.ToCell(hitPoint)) + new Vector3(0.5f, 0, 0.5f);
					string entityName = string.Empty;
					Block block = BlocksManager.Blocks[Terrain.ExtractContents(componentMiner.ActiveBlockValue)];
					switch (block)
					{
						case JackODummyBlock:
							entityName = "JackODummy";
							break;
					}
					if (!string.IsNullOrEmpty(entityName))
					{
						Entity entity = DatabaseManager.CreateEntity(base.Project, entityName, false);
						if (entity != null)
						{
							Vector3 forward = Matrix.CreateFromQuaternion(componentMiner.ComponentCreature.ComponentCreatureModel.EyeRotation).Forward;
							float num = Vector3.Dot(forward, Vector3.UnitZ);
							float num2 = Vector3.Dot(forward, Vector3.UnitX);
							float num3 = Vector3.Dot(forward, -Vector3.UnitZ);
							float num4 = Vector3.Dot(forward, -Vector3.UnitX);
							float rotateAngle = 0f;
							if (num == MathUtils.Max(num, num2, num3, num4))
								rotateAngle = 180f;
							else if (num2 == MathUtils.Max(num, num2, num3, num4))
								rotateAngle = -90f;
							else if (num3 == MathUtils.Max(num, num2, num3, num4))
								rotateAngle = 0f;
							else if (num4 == MathUtils.Max(num, num2, num3, num4))
								rotateAngle = 90f;

							Quaternion rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, MathUtils.DegToRad(rotateAngle));
							ComponentBody componentBody = entity.FindComponent<ComponentBody>(true);
							componentBody.Position = position;
							componentBody.Rotation = rotation;
							entity.FindComponent<ComponentSpawn>(true).SpawnDuration = 0;
							entity.FindComponent<ComponentJackODummy>()?.Rotation = rotation;
							base.Project.AddEntity(entity);
							componentMiner.RemoveActiveTool(1);
							AudioManager.PlaySound("Audio/BlockPlaced", 1f, 0f, 0f);
						}
						else
							componentMiner.ComponentPlayer?.ComponentGui.DisplaySmallMessage("entity not found!", Color.White, false, false);
						return true;
					}
				}
			}
			return false;
		}
	}
}
